package com.duing.util;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class MailUtil {

    private static String emailAccount = "972270262@qq.com";
    private static String emailPassword = "kkfcwcectynfbgae";

    private static String receiveAccount = "15062978829@163.com";

    public static void main(String[] args) throws Exception {
        send();
    }

    public static void send() throws Exception {
        Properties properties = new Properties();

        properties.setProperty("mail.transport.protocol", "smtp");

        properties.setProperty("mail.smtp.host", "smtp.qq.com");

        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.port", "465");

        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.port", "465");

        Session session = Session.getInstance(properties);

        session.setDebug(true);


        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(emailAccount, "主管", "UTF-8"));

        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveAccount, "王状", "UTF-8"));
        message.setSubject("测试邮件", "UTF-8");
        message.setContent("这是来自公司的一份offer", "text/html;charset=UTF-8");

        message.setSentDate(new Date());
        message.saveChanges();

        // 创建传输对象  建立连接  发送邮件
        Transport transport = session.getTransport();
        transport.connect(emailAccount, emailPassword);
        transport.sendMessage(message, message.getAllRecipients());

        transport.close();
    }
}
